//
//  VRLoginViewController.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/12.
//

import UIKit

class VRLoginViewController: VRBaseViewController {

    private let backView            = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Login_bg").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(30.font_bold)
        .alignment(.center).text("Welcome Back").end()
    private let subLabel            = c_make_ex(UILabel.self)
        .color(.white).font(16.font_medium)
        .alignment(.center).text("Login to your Account").end()
    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let emailTextF          = c_make_ex(UITextField.self)
        .bgColor(.white.alpha(0.1)).corner(25).font(14.font_medium)
        .placeholder("Your email").color(.white).end()
    private let passwordTextF       = c_make_ex(UITextField.self)
        .bgColor(.white.alpha(0.1)).corner(25).font(14.font_medium)
        .placeholder("Your password").color(.white).end()
    private let loginButton         = c_make_ex(UIButton.self)
        .bgColor("#FFFC00".toColor).corner(25).font(18.font_bold)
        .text("Log In").color(.black).end()
    private var rightButton = UIButton(type: .custom)
    private lazy var setUpLabel        = ActiveLabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.subviews{
            backView.subviews{
                backButton
                titleLabel
                subLabel
                emailTextF
                passwordTextF
                loginButton
                setUpLabel
            }
        }
        
        backView.fillContainer()
        backButton.top(.statusHeight + 2).leading(5).size(40)
        titleLabel.top(.navi_height + 150.0).centerHorizontally()
        subLabel.centerHorizontally()
        subLabel.Top == titleLabel.Bottom + 10
        emailTextF.width(305).height(50).centerHorizontally()
        emailTextF.Top == subLabel.Bottom + 50
        passwordTextF.width(305).height(50).centerHorizontally()
        passwordTextF.Top == emailTextF.Bottom + 30
        loginButton.width(320).height(50).centerHorizontally()
        loginButton.Top == passwordTextF.Bottom + 135
        setUpLabel.centerHorizontally()
        setUpLabel.Top == loginButton.Bottom + 20
        
        configSetupStyle()
        backView.isUserInteractionEnabled = true
        emailTextF.style { v in
            v.placehold(.white.alpha(0.2), font: 14.font_medium)
            let leftImage = UIImageView(image: UIImage(named: "welcome_email"))
            leftImage.frame = CGRect(x: 10, y: 5, width: 40, height: 40)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 55, height: 50))
            leftV.addSubview(leftImage)
            v.leftView = leftV
            v.leftViewMode = .always
        }
        passwordTextF.style { v in
            v.placehold(.white.alpha(0.2), font: 14.font_medium)
            let leftImage = UIImageView(image: UIImage(named: "welcome_lock"))
            leftImage.frame = CGRect(x: 10, y: 5, width: 40, height: 40)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 55, height: 50))
            leftV.addSubview(leftImage)
            v.leftView = leftV
            v.leftViewMode = .always
//            let rightButton = UIButton(type: .custom)
            rightButton.setImage("welcome_hidden".toImage, for: .normal)
            rightButton.setImage("welcome_show".toImage, for: .selected)
            rightButton.frame = CGRect(x: 0, y: 10, width: 30, height: 30)
            rightButton.addTarget(self, action: #selector(clickShowPassword), for: .touchUpInside)
            let rightV = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            rightV.addSubview(rightButton)
            v.rightView = rightV
            v.rightViewMode = .always
            v.isSecureTextEntry = true
        }
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
    }
    
    
    private func configSetupStyle() {
        let customType1 = ActiveType.custom(pattern: "Sign up")
        setUpLabel.isShowUnderLine = false
        setUpLabel.font = 14.font_medium
        setUpLabel.numberOfLines = 0
        setUpLabel.textAlignment = .center
        setUpLabel.enabledTypes = [customType1]
        setUpLabel.text = "Don’t have an account? Sign up"
        setUpLabel.textColor = "#FFFFFF".toColor
        setUpLabel.customColor[customType1] = "#FFFC00".toColor
        
        setUpLabel.handleCustomTap(for: customType1) { [weak self] str in
            
        }
    }

}

//MARK: action
extension VRLoginViewController {
    
    @objc private func clickShowPassword(){
        
        rightButton.isSelected = !rightButton.isSelected
        passwordTextF.isSecureTextEntry = !rightButton.isSelected
    }
    
    @objc private func clickLoginButton(){
        
        guard let email = emailTextF.text, email.isValidEmail else {
            "Invalid email address.".toast_error
            return
        }
        guard let password = passwordTextF.text, password.validatePassword() else{
             "The password should be 6-14 digits.".toast_error
            return
        }
        RT.appleLogininWith(emailNum: email, Password: password) { info in
            let str = GetInfo(key: "UserInfo") as! NSDictionary
            let jsonData = InfoUtil.initInfo.deserialize(from: str)
            USEINFOMODEL = jsonData!
            UIApplication.window?.rootViewController = VRTabBarViewController()
        }
    }
    
}
